/**
 * jsonp跨域
 * 只能实现get请求
 * 不安全，xss攻击
 */

function jsonp(url, params, callback) {
  return new Promise((resolve, reject) => {
    // 请求回调
    window[callback] = function(data) {
      resolve(data)
      this.document.body.removeChild(s)
    }

    let paramArr = [`callback=${callback}`]
    let paramStr = ''
    if (params) {
      for(let key in params) {
        paramArr.push(`${key}=${params(key)}`)
      }
      paramStr = paramArr.join('&')
    }

    let s = document.createElement('script')
    s.src = `${url}?${paramStr}`
    document.body.appendChild(s)
  })
}

jsonp({
  url: '',
  params: {
    name: 'xiaoming',
    id: 9527
  }
})